import { Component, OnInit, Injector, ViewEncapsulation, ElementRef, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    StockZT_RoomServiceProxy,
    StockZT_LocationServiceProxy,
    ListDtoExtensionsItem
} from '@shared/service-proxies/stock-service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { TreeNode } from 'primeng/api';
import { TreeDataHelperService } from '@shared/utils/tree-data-helper.service';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { finalize } from 'rxjs/operators';
import { getRootComponents } from '@angular/core/src/render3/discovery_utils';
import { CreateOrEditLocationModalComponent } from './create-or-edit-location-modal.component';
import { CheckboxModule } from 'primeng/checkbox';
import { IBasicRoomInfo } from './basic-room-info';
import { Base_DataItemDetailServiceProxy } from '@shared/service-proxies/stock-service-proxies';
@Component({ 
    selector: 'location',
    templateUrl: './location.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class LocationComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _LocationServiceProxy: StockZT_LocationServiceProxy,
        private _base_DataItemDetailManager: Base_DataItemDetailServiceProxy
    ) {
        super(injector);
    }
    f_WarehouseCode;
    f_StorageAreaNum;
    f_StorageAreaType;
    storageAreaTypeOptions;
    selectedValue;
    roomId;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditLocationModal') createOrEditLocationModal: CreateOrEditLocationModalComponent;
    extensions: Array<ListDtoExtensionsItem>;
    ngOnInit(): void {
        this._base_DataItemDetailManager.getDataItemDetailByItemId('StorageAreaType').subscribe(result => {
            this.storageAreaTypeOptions = result.items.map(e => {
                return {
                    label: e.displayName,
                    value: e.displayName
                };
            });
        });
    }

    getLocationData(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._LocationServiceProxy
            .getLocationListExtention(
                this.f_WarehouseCode,
                this.f_StorageAreaNum,
                this.f_StorageAreaType,
                null,
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.stock_LocationList.totalCount;
                this.primengTableHelper.records = result.stock_LocationList.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    deletePosition(id: string) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._LocationServiceProxy.deleteLocation(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }
}
